
/**
 * 防抖
 * @param {*} fn 
 * @param {*} wait 
 */
function setdebounce(fn,wait)
{
    var timer = null;
    return function() {
        if(timer !== null) {
            clearTimeout(timer);
        }
        timer = setTimeout(fn,wait);
    }
}

/**
 * 节流
 * @param {*} fn 
 * @param {*} delay 
 */
function setthrottle(fn,delay)
{
    var prev = Date.now();
    var first = 1;
    return function() {
        if(first) {
            console.log("is first");
            fn.call(this,arguments);
            first = 0;
        } else {
            var now = Date.now();
            if(now-prev >= delay) {
                console.log("is not first");
                fn.call(this,arguments);
                prev = Date.now();
            }
        }
    }
}


/**
 * 鼠标进入某些区域显示指定div
 * showel 要显示的div
 * els 鼠标进入的区域数组 
 * timeout 需要一定的延迟，否则失效
 */
function showelIfmouseEnterEls(showel,els,timeout = 100) {
    var inthese = 0;
    console.log(els instanceof Array );
    if(els instanceof Array !== true) {
        let tels = els;
        els = [];
        els.push(tels);
    }
    for(let el of els) {
        document.querySelector(el).addEventListener('mouseenter',function(){
            inthese = 1;
            document.querySelector(showel).style.display = 'block';
        });
        document.querySelector(el).addEventListener('mouseleave',function(){
            inthese = 0;
            setTimeout(()=>{
                if(!inthese) {
                    document.querySelector(showel).style.display = 'none';
                }
            },timeout);
        });
    }
}

export {
    setdebounce,
    setthrottle,
    showelIfmouseEnterEls
}